package cn.tedu.wenzhang2.base.exception;


import cn.tedu.wenzhang2.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobExceptionHandler {

    @ExceptionHandler
    public JsonResult doHandlerMethodArgumentNotValidException(MethodArgumentNotValidException e){
        String data=e.getFieldError().getDefaultMessage();
        log.debug("MethodArgumentNotValidException:"+data);

        return JsonResult.notok(data);
    }


    @ExceptionHandler
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException e){
        String data=e.getMessage().split(":")[1].trim();
        log.debug("ConstraintViolationException:"+data);

        return JsonResult.notok(data);
    }
}
